"""
验证迁移结果
"""

import os
import sys
import asyncio

# 设置环境变量
os.environ['SUPABASE_URL'] = 'https://wfmarihxfyyqljzaakbk.supabase.co'
os.environ['SUPABASE_ANON_KEY'] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6IndmbWFyaWh4Znl5cWxqemFha2JrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTcxNTM5MDQsImV4cCI6MjA3MjcyOTkwNH0.9KoJUGhMbMNyNZhFHaqUIv3mt4z26XnbN4sMq0XlUxo'

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

async def main():
    print("=== 验证迁移结果 ===")
    
    try:
        from database import get_db_service
        db_service = get_db_service()
        
        if not db_service.initialize():
            print("[ERROR] 数据库连接失败")
            return
            
        print("[OK] 数据库连接成功")
        
        # 验证会话数量
        sessions_result = await db_service.get_sessions(page=1, page_size=50)
        sessions_count = sessions_result['total']
        print(f"[INFO] 数据库中的会话数量: {sessions_count}")
        
        # 验证角色数量
        custom_roles = await db_service.get_all_custom_roles()
        roles_count = len(custom_roles)
        print(f"[INFO] 数据库中的自定义角色数量: {roles_count}")
        
        # 验证消息数量（通过查询几个会话）
        total_messages = 0
        for session in sessions_result['sessions'][:5]:  # 检查前5个会话
            messages = await db_service.get_messages(session.id)
            total_messages += len(messages)
            
        print(f"[INFO] 前5个会话的消息总数: {total_messages}")
        
        # 显示一些示例数据
        if sessions_result['sessions']:
            first_session = sessions_result['sessions'][0]
            print(f"[INFO] 第一个会话: {first_session.title}")
            
        if custom_roles:
            first_role = custom_roles[0]
            print(f"[INFO] 第一个角色: {first_role.name} - {first_role.title}")
            
        print("\\n=== 验证完成 ===")
        print("[SUCCESS] 数据已成功存储在Supabase云数据库中！")
        print("你可以在以下地址查看:")
        print("https://wfmarihxfyyqljzaakbk.supabase.co")
        
    except Exception as e:
        print(f"[ERROR] 验证失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    asyncio.run(main())